package controller;

import dao.PersionBeanDao;
import dao.RolePermissionDao;
import dao.UserDao;
import entity.Attendence;
import entity.Department;
import entity.Permission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import pojo.PersionBean;
import pojo.StateBean;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @version 1.0
 * @Author: 举个栗子吖
 * @Date: 2019/11/12 21:22
 */
@Controller
@RequestMapping("/user")
public class UserController {
    @Resource(name = "userDao")
    private UserDao userDao;
    @Resource(name = "persionBeanDao")
    private PersionBeanDao persionBeanDao;
    @Autowired
    private RolePermissionDao rolePermissionDao;

    @RequestMapping("/query")
    @ResponseBody
    public Map<String, Object> query(String userName, int offset, int limit) {
        Attendence attendence = new Attendence();
        attendence.setStaffName(userName);
        Map<String, Object> map = new HashMap<String, Object>();
        Map<String, Object> map1 = new HashMap<String, Object>();
        map.put("staffName", userName);
        map.put("offset", (offset - 1) * limit);
        map.put("limit", limit);
        List<Attendence> list = userDao.list(map);
        map1.put("total", userDao.count(attendence));
        map1.put("rows", list);
        return map1;
    }

    @RequestMapping("/select")
    @ResponseBody
    public List<Department> list() {
        return userDao.query();
    }

    @RequestMapping("/add")
    @ResponseBody
    public int add(String staffName, long sex, long depeld, Date data) {
        Attendence attendence = new Attendence();
        attendence.setStaffName(staffName);
        attendence.setSex(sex);
        attendence.setDeptld(depeld);
        attendence.setAttendDate(data);
        return userDao.add(attendence);
    }

    @RequestMapping("/getAll")
    @ResponseBody
    public List<Department> getAll(int id) {
//        查看用户当前职位
        Department department = userDao.getAll(id);
//          查看所有职位
        List<Department> list = userDao.getId();
        for (Department r : list) {
            if (department.getDeptid() == r.getDeptid()) {
                r.setFalg(true);
            }
        }
        return list;
    }

    @RequestMapping("/upd")
    @ResponseBody
    public int update(int id, int deptid) {
        return userDao.update(id, deptid);
    }

    @RequestMapping("/del")
    @ResponseBody
    public String del(int id) {
        int del = userDao.del(id);
        if (del > 0) {
            return "1";
        }
        return "0";
    }

    @RequestMapping("/delInfo")
    @ResponseBody
    public int delInfo(@RequestParam(required = false, value = "attendld[]") int[] attendld) {
        return userDao.getDel(attendld);
    }

    /*树形菜单*/
    @RequestMapping("/dofenpei")
    @ResponseBody
    public List<PersionBean> dofenpei(int sid) {
        /*得到当前角色所拥有的权限*/
        List<Permission> listAll = rolePermissionDao.queryByRoleId(sid);
        /*获取所有根节点*/
        List<PersionBean> list = persionBeanDao.query(sid);
        for (PersionBean persionBean : list) {
            int id = persionBean.getId();
            /*查询子节点*/
            List<PersionBean> list1 = persionBeanDao.query(id);
            persionBean.setNodes(list1);
            StateBean stateBean = new StateBean();
            persionBean.setState(stateBean);
        }
        /*回显当前角色所拥有的权限*/
        for (PersionBean per : list) {
            StateBean stateBean = new StateBean();
            for (Permission permission : listAll) {
                if (per.getId() == permission.getId()) {
                    stateBean.setChecked(true);
                }
            }
            per.setState(stateBean);
//            所有的子节点
            List<PersionBean> listSon = per.getNodes();
            for (PersionBean permission1 : listSon) {
                StateBean stateBean1 = new StateBean();
                for (Permission permission : listAll) {
                    if (permission1.getId() == permission.getId()) {
                        stateBean.setChecked(true);
                        stateBean1.setChecked(true);
                    }
                }
                permission1.setState(stateBean1);
            }
        }
        return list;
    }

}
